package com.huanletao.spring.aop;

import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * <p>
 *    StaticMethodMatcherPointcutAdvisor 定义匹配规则,设置advice 是拦截顶级接口, ;
 *    MethodInterceptor 是子接口， 将自己要拦截要做的功能，实现MethodInterceptor 接口,
 *
 * </p>
 *
 * @package: com.huanletao.spring.aop
 * @Description:
 * @className: Permission
 * @author: @NNCSKJ ADMIN
 * @copyright: Copyright (c) 2022/6/26 16:16
 * @version: V1.0
 */
public class PermissionMethodInterceptorAdvisor extends StaticMethodMatcherPointcutAdvisor {


    public PermissionMethodInterceptorAdvisor() {
       setAdvice(new PermissionMethodInterceptor());
    }

    @Override
    public boolean matches(Method method, Class targetClass) {
        Method m = method;
        if (isAuthzAnnotationPresent(m) ) {
            return true;
        }

//        if ( targetClass != null) {
//            try {
//                m = targetClass.getMethod(m.getName(), m.getParameterTypes());
//                return isAuthzAnnotationPresent(m);
//            } catch (NoSuchMethodException ignored) {
//
//            }
//        }
        return false;
    }

    private boolean isAuthzAnnotationPresent(Method method) {
        Annotation a = AnnotationUtils.findAnnotation(method, Permssion.class);
        return a!= null;
    }
}
